from __future__ import annotations

from typing import TYPE_CHECKING

from sqlalchemy import Boolean, Date, Enum, ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.db.base_class import BaseEntity

if TYPE_CHECKING:
  from .relationship import Relationship
  from .user import User


class Anniversary(BaseEntity):
  __tablename__ = "anniversaries"

  user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
  relationship_id: Mapped[int | None] = mapped_column(ForeignKey("relationships.id"), index=True)
  name: Mapped[str] = mapped_column(String(160))
  date: Mapped[Date] = mapped_column(Date)
  recurrence: Mapped[str] = mapped_column(
    Enum("once", "monthly", "yearly", "custom", name="anniversary_recurrence"), default="yearly"
  )
  reminder_offset_days: Mapped[int] = mapped_column(Integer, default=7)
  # Lunar support & rendering
  is_lunar: Mapped[bool] = mapped_column(Boolean, default=False)
  lunar_date_str: Mapped[str | None] = mapped_column(String(16))
  # Countdown modes: down(倒计时)、up(正计时)、both(同时显示)
  countdown_mode: Mapped[str] = mapped_column(
    Enum("down", "up", "both", name="anniversary_countdown_mode"), default="down"
  )

  user: Mapped["User"] = relationship(back_populates="anniversaries", lazy="joined")
  relationship: Mapped["Relationship | None"] = relationship("Relationship", lazy="joined")
